package Lab11.Q6;

public class ThreadPrint {
    public static void main(String[] args) throws InterruptedException{
        Object a=new Object();
        Object b=new Object();
        Object c=new Object();
        Thread8 threadA=new Thread8("A",c,a);
        Thread8 threadB=new Thread8("B",a,b);
        Thread8 threadC=new Thread8("C",b,c);
        new Thread(threadA).start();
        Thread.sleep(100);//A先停下，目前输出A,c.wait,a.notify
        new Thread(threadB).start();
        Thread.sleep(100);//B再停下，目前输出AB,a.wait,b.notify
        new Thread(threadC).start();
        Thread.sleep(100);//C再停下，输出ABC,b.wait,c.notify
    }
}
class Thread8 implements Runnable{
    private String name;
    private Object prev;
    private Object self;
    public Thread8(String name,Object prev,Object self){
        this.name=name;
        this.prev=prev;
        this.self=self;
    }
    @Override
    public void run(){
        int count=10;
        while(count>0){
            synchronized (prev){
                synchronized (self){
                    System.out.print(name);
                    count--;
                    self.notify();
                }
                try{
                    if(count==0)
                        break;
                    else
                        prev.wait();
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }
    }
}